package com.example.demo.utils.http;
//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//   ┃　　　┃   神兽保佑
//   ┃　　　┃   代码无BUG！
//   ┃　　　┗━━━┓
//   ┃　　　　　　　┣┓
//   ┃　　　　　　　┏┛
//   ┗┓┓┏━┳┓┏┛
//     ┃┫┫　┃┫┫
//     ┗┻┛　┗┻┛


import io.reactivex.rxjava3.functions.Function;
import okhttp3.ResponseBody;

/**
 * @function
 * @Author: jugg can
 * @Time: 14:54
 * @Version: 1.0.0
 */
public class HttpRequstUtils {
//
//    public static void post(String url, Object tag, HashMap<String, String> map, HttpCallback callback) {
////        if (!NetUtil.isNetAvailable()) {
////            ToastUtil.showToast(MyApplication.getInstance(), MyApplication.getInstance().getResources().getString(R.string.net_error_string), 1);
////            return;
////        }
////        post(url, tag, map, false, callback);
//
//
//    }
//
//    public static void post(String url, Object tag, Map<String, Object> map, boolean isAddToken, AutoDisposeConverter converter, final HttpCallback callback) {
////        map.put("clientType", "ANDROID");
////        if (isAddToken) {
////            map.put("token", "");
////        }
////        IHttpApiService service = HttpClient.getInstance().create(IHttpApiService.class);
////
////        service.getdata(url, map)
////                .subscribeOn(Schedulers.io())
////                .observeOn(AndroidSchedulers.mainThread())
//////                .as(converter)
////                .s
////                .subscribe(new Consumer<ResponseBody>() {
////
////                    @Override
////                    public void accept(ResponseBody responseBody) throws Exception {
////
////                    }
////
////                });
//
////        OkGo.getInstance().<String>post(url).upJson(jsonObject).tag(tag).execute(new StringCallback() {
////            @Override
////            public void onSuccess(Response<String> response) {
////                try {
////                    String body = response.body();
////                    MyLog.e("111", body + "");
////                    if (!TextUtils.isEmpty(response.body())) {
////                        JSONObject jsonObject = new JSONObject(body);
////                        if ("1".equals(jsonObject.getString("code"))) {
////                            if (jsonObject.isNull("data")) {
////                                //为了解决data 字段没有的情况
////                                callback.onSuccess("");
////                            } else {
////                                callback.onSuccess(jsonObject.getString("data"));
////                            }
////                        } else {
////                            callback.onError(jsonObject.getString("info") + "", jsonObject.getString("code"));
////                        }
////                    } else {
////                        callback.onError("数据为空！", "102");
////                    }
////                } catch (Exception e) {
////                    e.printStackTrace();
////                    callback.onError("数据为空！", "100");
////                }
////            }
////
////            @Override
////            public void onError(Response<String> response) {
////                super.onError(response);
////                callback.onFail(response.message(), response.code() + "");
////            }
////        });
//    }
//
//    public static Map<String, String> getRequst2(Map<String, String> params, Context context) {
//        if (params != null) {
//            final long lTimeCuo = System.currentTimeMillis();
//
//            params.put("Timestamp", lTimeCuo + "");
//            params.put("SerialNumber", SystemUtil.getDeviceImei(context));
////            JsonUtil.MD5(MyApplication.key + lTimeCuo)
//            params.put("Nonstr", Md5.encrypt(AppKeyConstant.NETKEY + lTimeCuo) + SharedPreferencesUtil.getString("userToken"));
//
//        }
//        return params;
//    }
//
//    public static Map<String, String> getRequst(Map<String, String> params, Context context) {
//        if (params != null) {
//            final long lTimeCuo = System.currentTimeMillis();
//
//            params.put("Timestamp", lTimeCuo + "");
//            params.put("SerialNumber", SystemUtil.getDeviceImei(context));
////            JsonUtil.MD5(MyApplication.key + lTimeCuo)
//            params.put("Nonstr", Md5.encrypt(AppKeyConstant.NETKEY + lTimeCuo));
//        }
//        return params;
//    }
//
//
//    public static Map<String, String> getRequst(Map<String, String> params) {
//        if (params != null) {
//            final long lTimeCuo = System.currentTimeMillis();
//            params.put("Timestamp", lTimeCuo + "");
////            JsonUtil.MD5(MyApplication.key + lTimeCuo)
//            params.put("Nonstr", Md5.encrypt(AppKeyConstant.NETKEY + lTimeCuo));
//        }
//        return params;
//    }

    //    {"StatusCode":200,"ErrorCode":0,"Data":[{"ID":69,"Title":"视频征集有奖活动","Content":"\u003cimg src=\"http://server.daiyudianjing.com/\\Image\\Activity\\132472930357276390.jpg\" alt=\"132472930357276390.jpg\"\u003e","CreateTime":"\/Date(1604286966623)\/","Author":"tangshanshan","Type":1,"Img":"132487605665761103.jpg",
//            "Sort":1,"IsShow":true,"Img2":null,"Img3":null,"IsShow2":false,"Img4":"132487605661542380.jpg"}],"Message":"成功","IsSuccess":true}
    // 统一rxjava 统一处理map转换
    public static Function<ResponseBody, String> getHttpMapFunction() {
        return new Function<ResponseBody, String>() {
            @Override
            public String apply(ResponseBody responseBody) throws Exception {
                String responesData = "";
                String body = responseBody.string();
//                if (!StringUtils.isEmpty(body)) {
//
//                    JsonObject obj = JsonParser.parseString(body).getAsJsonObject();
//                    JsonObject jsonObject = new JsonObject();
//                    if ("200".equals(jsonObject.getString("StatusCode")) && "成功".equals(jsonObject.getString("Message"))) {
//                        if (jsonObject.isNull("Data")) {
//                            //为了解决data 字段没有的情况
//                        } else {
//                            responesData = jsonObject.getString("Data");
//                        }
//                    } else {
//                        throw new Exception(jsonObject.getString("Message") + ":(" + jsonObject.getString("ErrorCode") + ")");
//                    }
//                } else {
//                    throw new Exception("数据为空!");
//                }
                return responesData;
            }
        };
    }

//    public static void analysisData(String data, HttpCallback callback) {
//        try {
//            String body = data;
//            if (!TextUtils.isEmpty(body)) {
//                JSONObject jsonObject = new JSONObject(body);
//                if ("1".equals(jsonObject.getString("code"))) {
//                    if (jsonObject.isNull("data")) {
//                        //为了解决data 字段没有的情况
//                        callback.onSuccess("");
//                    } else {
//                        callback.onSuccess(jsonObject.getString("data"));
//                    }
//                } else {
//                    callback.onError(jsonObject.getString("info") + "", jsonObject.getString("code"));
//                }
//            } else {
//                callback.onError("数据为空！", "102");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            callback.onError("数据为空！", "100");
//        }
//    }
}
